package sz.szsmk.citizencard.ui.newversion.activity.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.ui.newversion.bean.SchemeBusStep;
import sz.szsmk.citizencard.util.StringUtils;
import sz.szsmk.citizencard.util.zxyh.ResourceStyleUtils;


public class BusSegmentListAdapterNew extends RecyclerView.Adapter<BusSegmentListAdapterNew.BusSegmentViewHolder> {
    private Context mContext;
    private List<SchemeBusStep> mBusStepList = new ArrayList<SchemeBusStep>();
    private Drawable upDrawable;
    private Drawable downDrawable;
    private String destination;
    private String startPoint;


    public BusSegmentListAdapterNew(Context context,  String startPoint,String desPint) {
        this.mContext = context;
        this.destination = desPint;
        this.startPoint=startPoint;

    }

    @NonNull
    @Override
    public BusSegmentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bus_segment_new, parent, false);
        return new BusSegmentListAdapterNew.BusSegmentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BusSegmentViewHolder holder, int position) {
        final SchemeBusStep item = mBusStepList.get(position);
        if (position == 0) {
            holder.startLayout.setVisibility(View.VISIBLE);
            holder.detailStart.setText(StringUtils.isEmpty(startPoint)?"我的位置":startPoint);
        } else if (position == mBusStepList.size() - 1) {
            holder.endLayout.setVisibility(View.VISIBLE);
            holder.detailEnd.setText(StringUtils.isEmpty(this.destination)?"我的位置":this.destination);//输入目的地
        } else {
        }
        //布局不多且差异较大， 关闭复用，不然会有布局错乱的情况。
        holder.setIsRecyclable(false);
    }

    @Override
    public int getItemCount() {
        return mBusStepList.size();
    }

    public static class BusSegmentViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout startLayout;//开始的布局
        TextView detailStart;
        RelativeLayout endLayout;//目的地的布局
        TextView detailEnd;//目的地

        RelativeLayout walkLayout;//步行的布局
        TextView walkDistance;//步行多少米

        RelativeLayout detailItem;//火车 公交的布局
        ImageView itemType;
        TextView detailEntranceStation;//新街口
        TextView detailEntrance;//6号入口
        TextView detailLine;//1号线
        TextView detailDirection;//开往油坊桥
        TextView detailStationNum;//7站
        TextView detailStationTime;//20分钟
        TextView detailExit;//安德门
        boolean arrowExpend = false;
        LinearLayout expandContent;

        public BusSegmentViewHolder(View convertView) {
            super(convertView);
            startLayout = (RelativeLayout) convertView.findViewById(R.id.start_layout);
            detailStart=(TextView)convertView.findViewById(R.id.detail_start) ;
            startLayout.setVisibility(View.GONE);
            endLayout = (RelativeLayout) convertView.findViewById(R.id.end_layout);
            detailEnd = (TextView) convertView.findViewById(R.id.detail_end);
            endLayout.setVisibility(View.GONE);
            walkLayout = (RelativeLayout) convertView.findViewById(R.id.walk_layout);
            walkLayout.setVisibility(View.GONE);
            walkDistance = (TextView) convertView.findViewById(R.id.walk_distance);

            detailItem = (RelativeLayout) convertView.findViewById(R.id.detail_bus_item);
            detailItem.setVisibility(View.GONE);
            itemType = (ImageView) convertView.findViewById(R.id.item_type);
            detailEntranceStation = (TextView) convertView.findViewById(R.id.detail_entrance_station);
            detailEntrance = (TextView) convertView.findViewById(R.id.detail_entrance);
            detailLine = (TextView) convertView.findViewById(R.id.detail_line);
            detailDirection = (TextView) convertView.findViewById(R.id.detail_direction);
            detailStationNum = (TextView) convertView.findViewById(R.id.detail_station_num);
            detailStationTime = (TextView) convertView.findViewById(R.id.detail_station_time);
            detailExit = (TextView) convertView.findViewById(R.id.detail_exit);
            expandContent = (LinearLayout) convertView.findViewById(R.id.expand_content);
        }
    }

    private class ArrowClick implements OnClickListener {
        private BusSegmentViewHolder mHolder;
        private SchemeBusStep mItem;

        public ArrowClick(final BusSegmentViewHolder holder, final SchemeBusStep item) {
            mHolder = holder;
            mItem = item;

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int position = Integer.parseInt(String.valueOf(v.getTag()));
            mItem = mBusStepList.get(position);
            if (mItem.isBus()) {
                if (mHolder.arrowExpend == false) {
                    mHolder.arrowExpend = true;
                    upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                    mHolder.detailStationTime.setCompoundDrawables(null, null, upDrawable, null);
                    //	addBusStation(mItem.getBusLine().getDepartureBusStation());
                    //addBusStation(mItem.getBusLine().getArrivalBusStation());

                } else {
                    mHolder.arrowExpend = false;
                    downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                    mHolder.detailStationTime.setCompoundDrawables(null, null, downDrawable, null);
                    mHolder.expandContent.removeAllViews();
                }
            } else if (mItem.isRailway()) {
                if (mHolder.arrowExpend == false) {
                    mHolder.arrowExpend = true;
                    upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                    mHolder.detailStationTime.setCompoundDrawables(null, null, upDrawable, null);

                } else {
                    mHolder.arrowExpend = false;
                    downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                    mHolder.detailStationTime.setCompoundDrawables(null, null, downDrawable, null);
                    mHolder.expandContent.removeAllViews();
                }
            }


        }


    }

    public static String getRailwayTime(String time) {
        return time.substring(0, 2) + ":" + time.substring(2, time.length());
    }
}
